home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gdevpdfv.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  22.7 KB  |  713 lines

  1. /* Copyright (C) 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gdevpdfv.c,v 1.3.2.2 2000/11/09 23:24:18 rayjj Exp $ */
  20. /* Color value writing for pdfwrite driver */
  21. #include "math_.h"
  22. #include "string_.h"
  23. #include "gx.h"
  24. #include "gscindex.h"
  25. #include "gserrors.h"
  26. #include "gsiparm3.h"        /* for pattern colors */
  27. #include "gsmatrix.h"        /* for gspcolor.h */
  28. #include "gscoord.h"        /* for gs_currentmatrix, requires gsmatrix.h */
  29. #include "gsptype2.h"
  30. #include "gxcolor2.h"        /* for gxpcolor.h */
  31. #include "gxdcolor.h"        /* for gxpcolor.h */
  32. #include "gxpcolor.h"        /* for pattern device color types */
  33. #include "gxshade.h"
  34. #include "gdevpdfx.h"
  35. #include "gdevpdfg.h"
  36. #include "gdevpdfo.h"
  37. #include "szlibx.h"
  38.  
  39. /* Import the PatternType 2 Pattern device color type. */
  40. extern const gx_device_color_type_t gx_dc_pattern2;
  41.  
  42. /* ---------------- Utilities ---------------- */
  43.  
  44. /* Write a matrix parameter. */
  45. private int
  46. cos_dict_put_matrix(cos_dict_t *pscd, const char *key, const gs_matrix *pmat)
  47. {
  48.     float matrix[6];
  49.  
  50.     matrix[0] = pmat->xx;
  51.     matrix[1] = pmat->xy;
  52.     matrix[2] = pmat->yx;
  53.     matrix[3] = pmat->yy;
  54.     matrix[4] = pmat->tx;
  55.     matrix[5] = pmat->ty;
  56.     return cos_dict_put_c_key_floats(pscd, key, matrix, 6);
  57. }
  58.  
  59. /* ---------------- PatternType 1 colors ---------------- */
  60.  
  61. /*
  62.  * Create a Pattern resource referencing an image (currently only an XObject).
  63.  * p_tile is NULL for uncolored patterns or the NULL pattern.
  64.  * m_tile is NULL for colored patterns that fill their bounding box,
  65.  * including the NULL pattern.
  66.  ****** WE DON'T HANDLE NULL PATTERNS YET ******
  67.  */
  68. private uint
  69. tile_size(const gx_strip_bitmap *tile, int depth)
  70. {
  71.     return (tile->rep_width * depth + 7) / 8 * tile->rep_height;
  72. }
  73. private int
  74. pdf_pattern(gx_device_pdf *pdev, const gx_drawing_color *pdc,
  75.         const gx_color_tile *p_tile, const gx_color_tile *m_tile,
  76.         cos_stream_t *pcs_image, pdf_resource_t **ppres)
  77. {
  78.     pdf_resource_t *pres;
  79.     int code = pdf_alloc_resource(pdev, resourcePattern, pdc->mask.id, ppres,
  80.                   0L);
  81.     cos_stream_t *pcos;
  82.     cos_dict_t *pcd;
  83.     cos_dict_t *pcd_Resources = cos_dict_alloc(pdev, "pdf_pattern(Resources)");
  84.     const gx_color_tile *tile = (p_tile ? p_tile : m_tile);
  85.     const gx_strip_bitmap *btile = (p_tile ? &p_tile->tbits : &m_tile->tmask);
  86.     uint p_size =
  87.     (p_tile == 0 ? 0 : tile_size(&p_tile->tbits, pdev->color_info.depth));
  88.     uint m_size =
  89.     (m_tile == 0 ? 0 : tile_size(&m_tile->tmask, 1));
  90.     bool mask = p_tile == 0;
  91.     gs_point step;
  92.     gs_matrix smat;
  93.  
  94.     if (code < 0)
  95.     return code;
  96.     /*
  97.      * Acrobat Reader can't handle image Patterns with more than
  98.      * 64K of data.  :-(
  99.      */
  100.     if (max(p_size, m_size) > 65500)
  101.     return_error(gs_error_limitcheck);
  102.     /*
  103.      * We currently can't handle Patterns whose X/Y step isn't parallel
  104.      * to the coordinate axes.
  105.      */
  106.     if (is_xxyy(&tile->step_matrix))
  107.     step.x = tile->step_matrix.xx, step.y = tile->step_matrix.yy;
  108.     else if (is_xyyx(&tile->step_matrix))
  109.     step.x = tile->step_matrix.yx, step.y = tile->step_matrix.xy;
  110.     else
  111.     return_error(gs_error_rangecheck);
  112.     if (pcd_Resources == 0)
  113.     return_error(gs_error_VMerror);
  114.     gs_make_identity(&smat);
  115.     smat.xx = btile->rep_width / (pdev->HWResolution[0] / 72.0);
  116.     smat.yy = btile->rep_height / (pdev->HWResolution[1] / 72.0);
  117.     pres = *ppres;
  118.     {
  119.     cos_dict_t *pcd_XObject = cos_dict_alloc(pdev, "pdf_pattern(XObject)");
  120.     char key[MAX_REF_CHARS + 3];
  121.     cos_value_t v;
  122.  
  123.     if (pcd_XObject == 0)
  124.         return_error(gs_error_VMerror);
  125.     sprintf(key, "/R%ld", pcs_image->id);
  126.     COS_OBJECT_VALUE(&v, pcs_image);
  127.     if ((code = cos_dict_put(pcd_XObject, (byte *)key, strlen(key), &v)) < 0 ||
  128.         (code = cos_dict_put_c_key_object(pcd_Resources, "/XObject",
  129.                           COS_OBJECT(pcd_XObject))) < 0
  130.         )
  131.         return code;
  132.     }
  133.     if ((code = cos_dict_put_c_strings(pcd_Resources, "/ProcSet",
  134.                        (mask ? "[/PDF/ImageB]" :
  135.                     "[/PDF/ImageC]"))) < 0)
  136.     return code;
  137.     cos_become(pres->object, cos_type_stream);
  138.     pcos = (cos_stream_t *)pres->object;
  139.     pcd = cos_stream_dict(pcos);
  140.     if ((code = cos_dict_put_c_key_int(pcd, "/PatternType", 1)) < 0 ||
  141.     (code = cos_dict_put_c_key_int(pcd, "/PaintType",
  142.                        (mask ? 2 : 1))) < 0 ||
  143.     (code = cos_dict_put_c_key_int(pcd, "/TilingType",
  144.                        tile->tiling_type)) < 0 ||
  145.     (code = cos_dict_put_c_key_object(pcd, "/Resources",
  146.                       COS_OBJECT(pcd_Resources))) < 0 ||
  147.     (code = cos_dict_put_c_strings(pcd, "/BBox", "[0 0 1 1]")) < 0 ||
  148.     (code = cos_dict_put_matrix(pcd, "/Matrix", &smat)) < 0 ||
  149.     (code = cos_dict_put_c_key_real(pcd, "/XStep", step.x / btile->rep_width)) < 0 ||
  150.     (code = cos_dict_put_c_key_real(pcd, "/YStep", step.y / btile->rep_height)) < 0
  151.     ) {
  152.     return code;
  153.     }
  154.  
  155.     {
  156.     char buf[MAX_REF_CHARS + 6 + 1]; /* +6 for /R# Do\n */
  157.  
  158.     sprintf(buf, "/R%ld Do\n", pcs_image->id);
  159.     cos_stream_add_bytes(pcos, (const byte *)buf, strlen(buf));
  160.     }
  161.  
  162.     return 0;
  163. }
  164.  
  165. /* Set the ImageMatrix, Width, and Height for a Pattern image. */
  166. private void
  167. pdf_set_pattern_image(gs_data_image_t *pic, const gx_strip_bitmap *tile)
  168. {
  169.     pic->ImageMatrix.xx = pic->Width = tile->rep_width;
  170.     pic->ImageMatrix.yy = pic->Height = tile->rep_height;
  171. }
  172.  
  173. /* Write the mask for a Pattern. */
  174. private int
  175. pdf_put_pattern_mask(gx_device_pdf *pdev, const gx_color_tile *m_tile,
  176.              cos_stream_t **ppcs_mask)
  177. {
  178.     int w = m_tile->tmask.rep_width, h = m_tile->tmask.rep_height;
  179.     gs_image1_t image;
  180.     pdf_image_writer writer;
  181.     cos_stream_t *pcs_image;
  182.     long pos;
  183.     int code;
  184.  
  185.     gs_image_t_init_mask_adjust(&image, true, false);
  186.     pdf_set_pattern_image((gs_data_image_t *)&image, &m_tile->tmask);
  187.     if ((code = pdf_begin_write_image(pdev, &writer, gs_no_id, w, h, NULL, false)) < 0 ||
  188.     (pdev->params.MonoImage.Encode &&
  189.      (code = psdf_CFE_binary(&writer.binary, w, h, true)) < 0) ||
  190.     (code = pdf_begin_image_data(pdev, &writer, (const gs_pixel_image_t *)&image, NULL)) < 0
  191.     )
  192.     return code;
  193.     pcs_image = (cos_stream_t *)writer.pres->object;
  194.     pos = stell(pdev->streams.strm);
  195.     /* Pattern masks are specified in device coordinates, so invert Y. */
  196.     if ((code = pdf_copy_mask_bits(writer.binary.strm, m_tile->tmask.data + (h - 1) * m_tile->tmask.raster, 0, -m_tile->tmask.raster, w, h, 0)) < 0 ||
  197.     (code = cos_stream_add_since(pcs_image, pos)) < 0 ||
  198.     (code = pdf_end_image_binary(pdev, &writer, h)) < 0 ||
  199.     (code = pdf_end_write_image(pdev, &writer)) < 0
  200.     )
  201.     return code;
  202.     *ppcs_mask = pcs_image;
  203.     return 0;
  204. }
  205.  
  206. /* Write a colored Pattern color. */
  207. /* (Single-use procedure for readability.) */
  208. private int
  209. pdf_put_colored_pattern(gx_device_pdf *pdev, const gx_drawing_color *pdc,
  210.             const psdf_set_color_commands_t *ppscc,
  211.             pdf_resource_t **ppres)
  212. {
  213.     const gx_color_tile *m_tile = pdc->mask.m_tile;
  214.     const gx_color_tile *p_tile = pdc->colors.pattern.p_tile;
  215.     int w = p_tile->tbits.rep_width, h = p_tile->tbits.rep_height;
  216.     gs_color_space cs_Device;
  217.     cos_value_t cs_value;
  218.     pdf_image_writer writer;
  219.     gs_image1_t image;
  220.     cos_stream_t *pcs_image;
  221.     cos_stream_t *pcs_mask = 0;
  222.     cos_value_t v;
  223.     long pos;
  224.     int code = pdf_cs_Pattern_colored(pdev, &v);
  225.  
  226.     if (code < 0)
  227.     return code;
  228.     pdf_cspace_init_Device(&cs_Device, pdev->color_info.num_components);
  229.     code = pdf_color_space(pdev, &cs_value, &cs_Device,
  230.                &pdf_color_space_names, true);
  231.     if (code < 0)
  232.     return code;
  233.     gs_image_t_init_adjust(&image, &cs_Device, false);
  234.     image.BitsPerComponent = 8;
  235.     pdf_set_pattern_image((gs_data_image_t *)&image, &p_tile->tbits);
  236.     if (m_tile) {
  237.     if ((code = pdf_put_pattern_mask(pdev, m_tile, &pcs_mask)) < 0)
  238.         return code;
  239.     }
  240.     if ((code = pdf_begin_write_image(pdev, &writer, gs_no_id, w, h, NULL, false)) < 0 ||
  241.     (code = psdf_setup_lossless_filters((gx_device_psdf *)pdev,
  242.                         &writer.binary,
  243.                         (gs_pixel_image_t *)&image)) < 0 ||
  244.     (code = pdf_begin_image_data(pdev, &writer, (const gs_pixel_image_t *)&image, &cs_value)) < 0
  245.     )
  246.     return code;
  247.     pcs_image = (cos_stream_t *)writer.pres->object;
  248.     pos = stell(pdev->streams.strm);
  249.     /* Pattern masks are specified in device coordinates, so invert Y. */
  250.     if ((code = pdf_copy_color_bits(writer.binary.strm, p_tile->tbits.data + (h - 1) * p_tile->tbits.raster, 0, -p_tile->tbits.raster, w, h, pdev->color_info.depth >> 3)) < 0 ||
  251.     (code = cos_stream_add_since(pcs_image, pos)) < 0 ||
  252.     (code = pdf_end_image_binary(pdev, &writer, h)) < 0
  253.     )
  254.     return code;
  255.     pcs_image = (cos_stream_t *)writer.pres->object;
  256.     if ((pcs_mask != 0 &&
  257.      (code = cos_dict_put_c_key_object(cos_stream_dict(pcs_image), "/Mask",
  258.                        COS_OBJECT(pcs_mask))) < 0) ||
  259.     (code = pdf_end_write_image(pdev, &writer)) < 0
  260.     )
  261.     return code;
  262.     code = pdf_pattern(pdev, pdc, p_tile, m_tile, pcs_image, ppres);
  263.     if (code < 0)
  264.     return code;
  265.     cos_value_write(&v, pdev);
  266.     pprints1(pdev->strm, " %s", ppscc->setcolorspace);
  267.     return 0;
  268. }
  269.  
  270. /* Write an uncolored Pattern color. */
  271. /* (Single-use procedure for readability.) */
  272. private int
  273. pdf_put_uncolored_pattern(gx_device_pdf *pdev, const gx_drawing_color *pdc,
  274.               const psdf_set_color_commands_t *ppscc,
  275.               pdf_resource_t **ppres)
  276. {
  277.     const gx_color_tile *m_tile = pdc->mask.m_tile;
  278.     cos_value_t v;
  279.     stream *s = pdev->strm;
  280.     int code = pdf_cs_Pattern_uncolored(pdev, &v);
  281.     cos_stream_t *pcs_image;
  282.     gx_drawing_color dc_pure;
  283.     static const psdf_set_color_commands_t no_scc = {0, 0, 0};
  284.  
  285.     if (code < 0 ||
  286.     (code = pdf_put_pattern_mask(pdev, m_tile, &pcs_image)) < 0 ||
  287.     (code = pdf_pattern(pdev, pdc, NULL, m_tile, pcs_image, ppres)) < 0
  288.     )
  289.     return code;
  290.     cos_value_write(&v, pdev);
  291.     pprints1(s, " %s ", ppscc->setcolorspace);
  292.     color_set_pure(&dc_pure, gx_dc_pure_color(pdc));
  293.     psdf_set_color((gx_device_vector *)pdev, &dc_pure, &no_scc);
  294.     return 0;
  295. }
  296.  
  297. /* ---------------- PatternType 2 colors ---------------- */
  298.  
  299. /* Write parameters common to all Shadings. */
  300. private int
  301. pdf_put_shading_common(cos_dict_t *pscd, const gs_shading_t *psh)
  302. {
  303.     gs_shading_type_t type = ShadingType(psh);
  304.     const gs_color_space *pcs = psh->params.ColorSpace;
  305.     int code = cos_dict_put_c_key_int(pscd, "/ShadingType", (int)type);
  306.     cos_value_t cs_value;
  307.  
  308.     if (code < 0 ||
  309.     (psh->params.AntiAlias &&
  310.      (code = cos_dict_put_c_strings(pscd, "/AntiAlias", "true")) < 0) ||
  311.     (code = pdf_color_space(pscd->pdev, &cs_value, pcs,
  312.                 &pdf_color_space_names, false)) < 0 ||
  313.     (code = cos_dict_put_c_key(pscd, "/ColorSpace", &cs_value)) < 0
  314.     )
  315.     return code;
  316.     if (psh->params.Background) {
  317.     code = cos_dict_put_c_key_floats(pscd, "/Background",
  318.                    psh->params.Background->paint.values,
  319.                    gs_color_space_num_components(pcs));
  320.     if (code < 0)
  321.         return code;
  322.     }
  323.     if (psh->params.have_BBox) {
  324.     float bbox[4];
  325.  
  326.     bbox[0] = psh->params.BBox.p.x;
  327.     bbox[1] = psh->params.BBox.p.y;
  328.     bbox[2] = psh->params.BBox.q.x;
  329.     bbox[3] = psh->params.BBox.q.y;
  330.     code = cos_dict_put_c_key_floats(pscd, "/BBox", bbox, 4);
  331.     if (code < 0)
  332.         return code;
  333.     }
  334.     return 0;
  335. }
  336.  
  337. /* Write a linear (Axial / Radial) Shading. */
  338. private int
  339. pdf_put_linear_shading(cos_dict_t *pscd, const float *Coords,
  340.                int num_coords, const float *Domain /*[2]*/,
  341.                const gs_function_t *Function,
  342.                const bool *Extend /*[2]*/)
  343. {
  344.     int code = cos_dict_put_c_key_floats(pscd, "/Coords", Coords, num_coords);
  345.  
  346.     if (code < 0 ||
  347.     ((Domain[0] != 0 || Domain[1] != 1) &&
  348.      (code = cos_dict_put_c_key_floats(pscd, "/Domain", Domain, 2)) < 0)
  349.     )
  350.     return code;
  351.     if (Function) {
  352.     cos_value_t fn_value;
  353.  
  354.     if ((code = pdf_function(pscd->pdev, Function, &fn_value)) < 0 ||
  355.         (code = cos_dict_put_c_key(pscd, "/Function", &fn_value)) < 0
  356.         )
  357.         return code;
  358.     }
  359.     if (Extend[0] | Extend[1]) {
  360.     char extend_str[1 + 5 + 1 + 5 + 1 + 1]; /* [bool bool] */
  361.  
  362.     sprintf(extend_str, "[%s %s]",
  363.         (Extend[0] ? "true" : "false"),
  364.         (Extend[1] ? "true" : "false"));
  365.     code = cos_dict_put_c_key_string(pscd, "/Extend",
  366.                      (const byte *)extend_str,
  367.                      strlen(extend_str));
  368.     }
  369.     return code;
  370. }
  371.  
  372. /* Write a scalar (non-mesh) Shading. */
  373. /* (Single-use procedure for readability.) */
  374. private int
  375. pdf_put_scalar_shading(cos_dict_t *pscd, const gs_shading_t *psh)
  376. {
  377.     int code = pdf_put_shading_common(pscd, psh);
  378.  
  379.     if (code < 0)
  380.     return code;
  381.     switch (ShadingType(psh)) {
  382.     case shading_type_Function_based: {
  383.     const gs_shading_Fb_params_t *const params =
  384.         (const gs_shading_Fb_params_t *)&psh->params;
  385.     cos_value_t fn_value;
  386.  
  387.     if ((code = cos_dict_put_c_key_floats(pscd, "/Domain", params->Domain, 4)) < 0 ||
  388.         (code = pdf_function(pscd->pdev, params->Function, &fn_value)) < 0 ||
  389.         (code = cos_dict_put_c_key(pscd, "/Function", &fn_value)) < 0 ||
  390.         (code = cos_dict_put_matrix(pscd, "/Matrix", ¶ms->Matrix)) < 0
  391.         )
  392.         return code;
  393.     return 0;
  394.     }
  395.     case shading_type_Axial: {
  396.     const gs_shading_A_params_t *const params =
  397.         (const gs_shading_A_params_t *)&psh->params;
  398.  
  399.     return pdf_put_linear_shading(pscd, params->Coords, 4,
  400.                       params->Domain, params->Function,
  401.                       params->Extend);
  402.     }
  403.     case shading_type_Radial: {
  404.     const gs_shading_R_params_t *const params =
  405.         (const gs_shading_R_params_t *)&psh->params;
  406.  
  407.     return pdf_put_linear_shading(pscd, params->Coords, 6,
  408.                       params->Domain, params->Function,
  409.                       params->Extend);
  410.     }
  411.     default:
  412.     return_error(gs_error_rangecheck);
  413.     }
  414. }
  415.  
  416. /* Add an integer range to an array. */
  417. private int
  418. pdf_array_add_int2(cos_array_t *pca, int lower, int upper)
  419. {
  420.     int code = cos_array_add_int(pca, lower);
  421.  
  422.     if (code >= 0)
  423.     code = cos_array_add_int(pca, upper);
  424.     return code;
  425. }
  426.  
  427. /* Put a clamped value into a data stream.  num_bytes < sizeof(int). */
  428. private void
  429. put_clamped(byte *p, floatp v, int num_bytes)
  430. {
  431.     int limit = 1 << (num_bytes * 8);
  432.     int i, shift;
  433.  
  434.     if (v <= -limit)
  435.     i = -limit + 1;
  436.     else if (v >= limit)
  437.     i = limit - 1;
  438.     else
  439.     i = (int)v;
  440.     for (shift = (num_bytes - 1) * 8; shift >= 0; shift -= 8)
  441.     *p++ = (byte)(i >> shift);
  442. }
  443. inline private void
  444. put_clamped_coord(byte *p, floatp v, int num_bytes)
  445. {
  446.     put_clamped(p, (v + 32768) * 0xffffff / 65535, num_bytes);
  447. }
  448.  
  449. /* Convert floating-point mesh data to packed binary. */
  450. /* BitsPerFlag = 8, BitsPerCoordinate = 24, BitsPerComponent = 16, */
  451. /* scaling is as defined below. */
  452. private int
  453. put_float_mesh_data(cos_stream_t *pscs, shade_coord_stream_t *cs,
  454.             int flag, int num_pts, int num_components,
  455.             bool is_indexed)
  456. {
  457.     byte b[1 + (3 + 3) * 16];    /* flag + x + y or c */
  458.     gs_fixed_point pts[16];
  459.     int i;
  460.     int code;
  461.  
  462.     b[0] = (byte)flag;        /* may be -1 */
  463.     if ((code = shade_next_coords(cs, pts, num_pts)) < 0)
  464.     return code;
  465.     for (i = 0; i < num_pts; ++i) {
  466.     put_clamped_coord(b + 1 + i * 6, fixed2float(pts[i].x), 3);
  467.     put_clamped_coord(b + 4 + i * 6, fixed2float(pts[i].y), 3);
  468.     }
  469.     if ((code = cos_stream_add_bytes(pscs, b + (flag < 0),
  470.                      (flag >= 0) + num_pts * 6)) < 0)
  471.     return code;
  472.     for (i = 0; i < num_components; ++i) {
  473.     float c;
  474.  
  475.     cs->get_decoded(cs, 0, NULL, &c);
  476.     put_clamped(b, (is_indexed ? c + 32768 : (c + 256) * 65535 / 511), 2);
  477.     if ((code = cos_stream_add_bytes(pscs, b, 2)) < 0)
  478.         return code;
  479.     }
  480.     return 0;
  481. }
  482.  
  483. /* Write a mesh Shading. */
  484. /* (Single-use procedure for readability.) */
  485. private int
  486. pdf_put_mesh_shading(cos_stream_t *pscs, const gs_shading_t *psh)
  487. {
  488.     cos_dict_t *const pscd = cos_stream_dict(pscs);
  489.     gs_color_space *pcs = psh->params.ColorSpace;
  490.     const gs_shading_mesh_params_t *const pmp =
  491.     (const gs_shading_mesh_params_t *)&psh->params;
  492.     int code = pdf_put_shading_common(pscd, psh);
  493.     int bits_per_coordinate, bits_per_component, bits_per_flag;
  494.     int num_comp = (pmp->Function ? 1 : gs_color_space_num_components(pcs));
  495.     bool from_array = data_source_is_array(pmp->DataSource);
  496.     bool is_indexed;
  497.     shade_coord_stream_t cs;
  498.     gs_matrix_fixed ctm_ident;
  499.     int flag;
  500.  
  501.     if (code < 0)
  502.     return code;
  503.  
  504.     /* Write parameters common to all mesh Shadings. */
  505.     shade_next_init(&cs, pmp, NULL);
  506.     if (from_array) {
  507.     cos_array_t *pca = cos_array_alloc(pscd->pdev, "pdf_put_mesh_shading");
  508.     int i;
  509.  
  510.     if (pca == 0)
  511.         return_error(gs_error_VMerror);
  512.     for (i = 0; i < 2; ++i)
  513.         if ((code = pdf_array_add_int2(pca, -32768, 32767)) < 0)
  514.         return code;
  515.     if (gs_color_space_get_index(pcs) == gs_color_space_index_Indexed) {
  516.         is_indexed = true;
  517.         if ((code = pdf_array_add_int2(pca, -32768, 32767)) < 0)
  518.         return code;
  519.     } else {
  520.         is_indexed = false;
  521.         for (i = 0; i < num_comp; ++i)
  522.         if ((code = pdf_array_add_int2(pca, -256, 255)) < 0)
  523.             return code;
  524.     }
  525.     code = cos_dict_put_c_key_object(pscd, "/Decode", COS_OBJECT(pca));
  526.     if (code < 0)
  527.         return code;
  528.     bits_per_coordinate = 24;
  529.     bits_per_component = 16;
  530.     bits_per_flag = 8;
  531.     gs_make_identity((gs_matrix *)&ctm_ident);
  532.     ctm_ident.tx_fixed = ctm_ident.ty_fixed = 0;
  533.     cs.pctm = &ctm_ident;
  534.     } else {
  535.     byte buf[100];        /* arbitrary */
  536.     uint num_read;
  537.  
  538.     code = cos_dict_put_c_key_floats(pscd, "/Decode", pmp->Decode,
  539.                 4 + gs_color_space_num_components(pcs) * 2);
  540.     while (sgets(cs.s, buf, sizeof(buf), &num_read), num_read > 0)
  541.         if ((code = cos_stream_add_bytes(pscs, buf, num_read)) < 0)
  542.         return code;
  543.     bits_per_coordinate = pmp->BitsPerCoordinate;
  544.     bits_per_component = pmp->BitsPerComponent;
  545.     bits_per_flag = -1;
  546.     }
  547.     if (code < 0 ||
  548.     (code = cos_dict_put_c_key_int(pscd, "/BitsPerCoordinate",
  549.                        bits_per_coordinate)) < 0 ||
  550.     (code = cos_dict_put_c_key_int(pscd, "/BitsPerComponent",
  551.                        bits_per_component)) < 0
  552.     )
  553.     return code;
  554.  
  555.     switch (ShadingType(psh)) {
  556.     case shading_type_Free_form_Gouraud_triangle: {
  557.     const gs_shading_FfGt_params_t *const params =
  558.         (const gs_shading_FfGt_params_t *)pmp;
  559.  
  560.     if (from_array)
  561.         while ((flag = shade_next_flag(&cs, 0)) >= 0)
  562.         if ((code = put_float_mesh_data(pscs, &cs, flag, 1, num_comp,
  563.                         is_indexed)) < 0)
  564.             return code;
  565.     if (bits_per_flag < 0)
  566.         bits_per_flag = params->BitsPerFlag;
  567.     break;
  568.     }
  569.     case shading_type_Lattice_form_Gouraud_triangle: {
  570.     const gs_shading_LfGt_params_t *const params =
  571.         (const gs_shading_LfGt_params_t *)pmp;
  572.  
  573.     if (from_array)
  574.         while (!seofp(cs.s))
  575.         if ((code = put_float_mesh_data(pscs, &cs, -1, 1, num_comp,
  576.                         is_indexed)) < 0)
  577.             return code;
  578.     return cos_dict_put_c_key_int(pscd, "/VerticesPerRow",
  579.                       params->VerticesPerRow);
  580.     }
  581.     case shading_type_Coons_patch: {
  582.     const gs_shading_Cp_params_t *const params =
  583.         (const gs_shading_Cp_params_t *)pmp;
  584.  
  585.     if (from_array)
  586.         while ((flag = shade_next_flag(&cs, 0)) >= 0) {
  587.         int num_c = (flag == 0 ? 4 : 2);
  588.  
  589.         if ((code = put_float_mesh_data(pscs, &cs, flag, 4 + num_c * 2,
  590.                         num_comp * num_c,
  591.                         is_indexed)) < 0)
  592.             return code;
  593.         }
  594.     if (bits_per_flag < 0)
  595.         bits_per_flag = params->BitsPerFlag;
  596.     break;
  597.     }
  598.     case shading_type_Tensor_product_patch: {
  599.     const gs_shading_Tpp_params_t *const params =
  600.         (const gs_shading_Tpp_params_t *)pmp;
  601.  
  602.     if (from_array)
  603.         while ((flag = shade_next_flag(&cs, 0)) >= 0) {
  604.         int num_c = (flag == 0 ? 4 : 2);
  605.  
  606.         if ((code = put_float_mesh_data(pscs, &cs, flag, 8 + num_c * 2,
  607.                         num_comp * num_c,
  608.                         is_indexed)) < 0)
  609.             return code;
  610.         }
  611.     if (bits_per_flag < 0)
  612.         bits_per_flag = params->BitsPerFlag;
  613.     break;
  614.     }
  615.     default:
  616.     return_error(gs_error_rangecheck);
  617.     }
  618.     return cos_dict_put_c_key_int(pscd, "/BitsPerFlag", bits_per_flag);
  619. }
  620.  
  621. /* Write a PatternType 2 (shading pattern) color. */
  622. /* (Single-use procedure for readability.) */
  623. private int
  624. pdf_put_pattern2(gx_device_pdf *pdev, const gx_drawing_color *pdc,
  625.          const psdf_set_color_commands_t *ppscc,
  626.          pdf_resource_t **ppres)
  627. {
  628.     const gs_pattern2_instance_t *pinst =
  629.     (gs_pattern2_instance_t *)pdc->ccolor.pattern;
  630.     const gs_shading_t *psh = pinst->template.Shading;
  631.     cos_value_t v;
  632.     pdf_resource_t *pres;
  633.     pdf_resource_t *psres;
  634.     cos_dict_t *pcd;
  635.     cos_object_t *psco;
  636.     int code = pdf_cs_Pattern_colored(pdev, &v);
  637.     gs_matrix smat;
  638.  
  639.     if (code < 0)
  640.     return code;
  641.     code = pdf_alloc_resource(pdev, resourcePattern, gs_no_id, ppres, 0L);
  642.     if (code < 0)
  643.     return code;
  644.     pres = *ppres;
  645.     cos_become(pres->object, cos_type_dict);
  646.     pcd = (cos_dict_t *)pres->object;
  647.     code = pdf_alloc_resource(pdev, resourceShading, gs_no_id, &psres, 0L);
  648.     if (code < 0)
  649.     return code;
  650.     psco = psres->object;
  651.     if (ShadingType(psh) >= 4) {
  652.     /* Shading has an associated data stream. */
  653.     cos_become(psco, cos_type_stream);
  654.     code = pdf_put_mesh_shading((cos_stream_t *)psco, psh);
  655.     } else {
  656.     cos_become(psco, cos_type_dict);
  657.     code = pdf_put_scalar_shading((cos_dict_t *)psco, psh);
  658.     }
  659.     /*
  660.      * In PDF, the Matrix is the transformation from the pattern space to
  661.      * the *default* user coordinate space, not the current space.
  662.      */
  663.     gs_currentmatrix(pinst->saved, &smat);
  664.     {
  665.     double xscale = 72.0 / pdev->HWResolution[0],
  666.         yscale = 72.0 / pdev->HWResolution[1];
  667.  
  668.     smat.xx *= xscale, smat.yx *= xscale, smat.tx *= xscale;
  669.     smat.xy *= yscale, smat.yy *= yscale, smat.ty *= yscale;
  670.     }
  671.     if (code < 0 ||
  672.     (code = cos_dict_put_c_key_int(pcd, "/PatternType", 2)) < 0 ||
  673.     (code = cos_dict_put_c_key_object(pcd, "/Shading", psco)) < 0 ||
  674.     (code = cos_dict_put_matrix(pcd, "/Matrix", &smat)) < 0
  675.     /****** ExtGState ******/
  676.     )
  677.     return code;
  678.     cos_value_write(&v, pdev);
  679.     pprints1(pdev->strm, " %s", ppscc->setcolorspace);
  680.     return 0;
  681. }
  682.  
  683. /* ---------------- Public procedure ---------------- */
  684.  
  685. /* Write a color value.  rgs is "rg" for fill, "RG" for stroke. */
  686. int
  687. pdf_put_drawing_color(gx_device_pdf *pdev, const gx_drawing_color *pdc,
  688.               const psdf_set_color_commands_t *ppscc)
  689. {
  690.     if (gx_dc_is_pure(pdc))
  691.     return psdf_set_color((gx_device_vector *) pdev, pdc, ppscc);
  692.     else {
  693.     /* We never halftone, so this must be a Pattern. */
  694.     int code;
  695.     pdf_resource_t *pres;
  696.     cos_value_t v;
  697.  
  698.     if (pdc->type == gx_dc_type_pattern)
  699.         code = pdf_put_colored_pattern(pdev, pdc, ppscc, &pres);
  700.     else if (pdc->type == &gx_dc_pure_masked)
  701.         code = pdf_put_uncolored_pattern(pdev, pdc, ppscc, &pres);
  702.     else if (pdc->type == &gx_dc_pattern2)
  703.         code = pdf_put_pattern2(pdev, pdc, ppscc, &pres);
  704.     else
  705.         return_error(gs_error_rangecheck);
  706.     if (code < 0)
  707.         return code;
  708.     cos_value_write(cos_resource_value(&v, pres->object), pdev);
  709.     pprints1(pdev->strm, " %s\n", ppscc->setcolorn);
  710.     return 0;
  711.     }
  712. }
  713.